<?php
namespace App\Service\Admin;

class AdminService extends BaseService
{
    //管理员

    //列表
    public function getIndex($token)
    {
        $adminOne = $this->adminByToken($token);
        $db = $this->dbConn($this->dbAdmin)->where('del', 0);
        if ($adminOne && $adminOne['role']==1) {
            $db = $db->where('role', 1)->where('id', $adminOne['id']);
        }
        $total = $db->count();
        $res = $db->get();
        if (!count($res)) {
            return $this->toback(-2, '没有记录');
        }
        $arys = [];
        foreach ($res as $v) {
            $v = (array) $v;
            $role_name = $v['role']==2 ? '超级' : '普通';
            $arys[] = [
                'id' => $v['id'],
                'name' => $v['name'],
                'role' => $v['role'],
                'role_name' => $role_name,
                'disable' => $v['disable'],
                'able_name' => $v['disable'] ? '禁用' : '启用',
                'uptime' => $v['uptime'],
            ];
        }
        return $this->toback(0, '', $arys, $total);
    }

    //详情
    public function getOne($id)
    {
        $res = (array) $this->dbConn($this->dbAdmin)
            ->where('id', $id)
            ->first();
        if (empty($res)) {
            return $this->toback(-2, '没有记录');
        }
        return $this->toback(0, '', $res);
    }

    //增加
    public function add($name)
    {
        $res = (array) $this->dbConn($this->dbAdmin)
            ->where('del', 0)
            ->where('name', $name)
            ->orderBy('id', 'desc')
            ->first();
        if (!empty($res)) {
            return $this->toback(-2, '此管理员已存在');
        }
        $pas = [
            'name' => $name, 'passwd' => md5('123456'),
        ];
        $pas['addtime'] = $pas['uptime'] = toDate();
        $up = $this->dbConn($this->dbAdmin)->insertGetId($pas);
        if (!$up) {
            return $this->toback(-1, 'error');
        }
        return $this->toback();
    }

    //更新
    public function modify($id, $name)
    {
        $res = (array) $this->dbConn($this->dbAdmin)
            ->where('id', $id)
            ->first();
        if (empty($res)) {
            return $this->toback(-2, '没有记录');
        }
        $res = (array) $this->dbConn($this->dbAdmin)
            ->where('name', $name)
            ->where('id', '<>', $id)
            ->first();
        if (empty($res)) {
            return $this->toback(-2, '手机号已被使用');
        }
        $up = $this->dbConn($this->dbAdmin)->where('id', $id)->update([
            'name' => $name, 'uptime' => toDate(),
        ]);
        if (!$up) {
            return $this->toback(-1, 'error');
        }
        return $this->toback();
    }

    //修改密码
    public function setPwd($id, $pas)
    {
        $res = (array) $this->dbConn($this->dbAdmin)
            ->where('id', $id)
            ->first();
        if (empty($res)) {
            return $this->toback(-2, '没有记录');
        }
        if ($res['passwd'] != md5($pas['pwdold'])) {
            return $this->toback(-2, '老密码错误');
        }
        $up = $this->dbConn($this->dbAdmin)->where('id', $id)->update([
            'passwd' => md5($pas['pwdnew']), 'uptime' => toDate(),
        ]);
        if (!$up) {
            return $this->toback(-1, 'error');
        }
        return $this->toback();
    }

    /**
     * 登录
     * @param string $uname
     * @param string $pwd
     */
    public function dologin($uname, $pwd)
    {
        $res = (array) $this->dbConn($this->dbAdmin)
            ->where('del', 0)
            //->where('disable', 0)
            ->where('name', $uname)
            ->first();
        if (empty($res)) {
            return $this->toback(-2, '没有记录');
        }
        if ($res['disable']) {
            return $this->toback(-2, '已被禁用');
        }
        if ($res['passwd'] != md5($pwd)) {
            return $this->toback(-2, '密码错误');
        }
        $token = toToken();
        //刷新日志
        $this->dbConn($this->dbAdmLogin)->insertGetId([
            'adminid' => $res['id'], 'admin_name' => $uname,
            'token' => $token, 'addtime' => toDate(),
        ]);
        $resData = [
            'token' => $token,
            'admin_name' => $uname,
            'role' => $res['role'],
        ];
        return $this->toback(0, '', $resData);
    }

    //删除
    public function del($id)
    {
        $res = (array) $this->dbConn($this->dbAdmin)
            ->where('del', 0)
            ->where('id', $id)
            ->first();
        if (empty($res)) {
            return $this->toback(-2, '没有记录');
        }
        $this->dbConn($this->dbAdmin)->where('id', $id)->update(['del' => 1]);
        return $this->toback();
    }

    //禁用启用
    public function able($id, $able)
    {
        $res = (array) $this->dbConn($this->dbAdmin)
            ->where('del', 0)
            ->where('id', $id)
            ->first();
        if (empty($res)) {
            return $this->toback(-2, '没有记录');
        }
        $this->dbConn($this->dbAdmin)
            ->where('id', $id)
            ->update(['disable' => $able]);
        return $this->toback();
    }
}